package com.chat.admin.controller;


import com.chat.admin.annotate.IsNumeric;
import com.chat.admin.domain.DTO.ADD.AddAuthorityDTO;
import com.chat.admin.domain.DTO.AuthorityDTO;
import com.chat.admin.domain.DTO.EDIT.EditAuthorityDTO;
import com.chat.admin.domain.VO.AuthorityVO;
import com.chat.admin.service.IAuthorityService;
import com.chat.common.entity.Authority;
import com.chat.common.entity.Role;
import com.chat.common.form.ListForm;
import com.chat.common.vo.R;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理员权限模块
 */
@RestController
@Validated
@RequestMapping("/admin/system/authority")
public class AuthorityController {

    @Autowired
    IAuthorityService iAuthorityService;

    /**
     * 获取权限列表
     * @param authorityDTO
     * @return
     */
    @PostMapping("/list")
    public R<ListForm<AuthorityVO>> getRoleList(@Validated @RequestBody AuthorityDTO authorityDTO) {
        return iAuthorityService.getAuthorityList(authorityDTO);
    }

    /**
     * 添加新的权限
     * @param addAuthorityDTO
     * @return
     */
    @PostMapping("/add")
    public R insertAuthority(@Validated @RequestBody AddAuthorityDTO addAuthorityDTO) {
        return iAuthorityService.insertAuthority(addAuthorityDTO);
    }

    /**
     * 修改权限
     * @param editAuthorityDTO
     * @return
     */
    @PostMapping("/Edit")
    public R updateAuthority(@Validated @RequestBody EditAuthorityDTO editAuthorityDTO) {
        return iAuthorityService.updateAuthority(editAuthorityDTO);
    }

    /**
     * 禁用权限
     * @param ID
     * @return
     */
    @GetMapping("/disableAuthority")
    public R<?> DisableAccount(@RequestParam @NotBlank(message = "ID不能为空") @IsNumeric(message = "ID不是数字") String ID) {
        return iAuthorityService.DisableAuthority(Long.valueOf(ID));
    }

    /**
     * 启用权限
     * @param ID
     * @return
     */
    @GetMapping("/enableAuthority")
    public R<?> EnableAccount(@RequestParam @NotBlank(message = "ID不能为空") @IsNumeric(message = "ID不是数字") String ID){
        return iAuthorityService.EnableAuthority(Long.valueOf(ID));
    }

    /**
     * 获取可用的权限List
     * @return
     */
    @GetMapping("/available_auth_list")
    public R<List<Authority>> getStateRoleList(){
        return R.succeed(iAuthorityService.getStateAuthorities());
    }

    /**
     * 获取可用权限的map
     * @return
     */
    @GetMapping("/available_auth_map")
    public R<Map<Long, Authority>> getStateRoleMap(){
        return R.succeed(iAuthorityService.getStateAuthoritiesMap());
    }
}
